package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import com.itheima.utnil.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> emplist = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) emplist;

        return new PageResult(p.getTotal(),p.getResult());

    }

    //增加数据
    @Override
    public void save(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        //上面的步骤返回id之后
        Integer id = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(new Consumer<EmpExpr>() {
            @Override
            public void accept(EmpExpr empExpr) {
                empExpr.setEmpId(id);
            }
        });
        empExprMapper.insertBatch(exprList);
    }

    //作业一：查询全部员工
    @Override
    public List<Emp> selectAll() {

        return empMapper.selectAll();
    }
    //登录
    @Override
    public LoginInfo login(Emp emp) {
        //1、查询数据库表emp
        Emp emplogin=   empMapper.getUsename(emp);
        //2、登录成功，将Emp转成LoginInfo
        if (emplogin!=null){
            //创建令牌载荷
            Map<String,Object>map=new HashMap<>();
            map.put("id",emplogin.getId());
            map.put("username",emplogin.getUsername());
            return new LoginInfo(emplogin.getId(), emplogin.getUsername(), emplogin.getName(), JwtUtils.generateJwt(map));


        }
        //3、登录失败，返回nuLL
        return null;
    }

    //回显
    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }
    //修改
    @Override
    public void update(Emp emp) {

        //1、根据ID修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //2、删除该员工的工作经历记录
        List<Integer> ids= Arrays.asList(emp.getId());
        empExprMapper.deleteByEmpIds(ids);

        //3、批量新增该员工的工作经历
        List<EmpExpr> exprList=emp.getExprList();
        if (exprList!=null){
            exprList.forEach(e->e.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);

        }

    }
    //    批量删除

    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteById(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

}







